package com.rwx.myjetpack.player

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import com.rwx.myjetpack.R
import com.rwx.myjetpack.base.BaseActivity
import com.rwx.myjetpack.databinding.ActivityPlayerBinding
import com.rwx.myjetpack.musiclist.MusicListPresenter

class PlayerActivity : BaseActivity()/**, IPlayerCallback*/ {

    val binding by lazy {
        ActivityPlayerBinding.inflate(layoutInflater)
    }

    val presenter by lazy {
        PlayerPresenter.instance
    }
    val musicPresenter by lazy { MusicListPresenter(this) }

//    init {
//        //addLifeListener(presenter)
//        lifeProvider.addLifeListener(presenter)
//        lifeProvider.addLifeListener(musicPresenter)
//
//    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

//        presenter.registerCallback(this)
//
        initListener()

        initDataListener()

    }

    private fun initListener() {

        binding.playOrPause.setOnClickListener {
            presenter.doPlayOrPause()
        }
        binding.playNext.setOnClickListener {
            presenter.doPlayNext()
        }
        binding.playPre.setOnClickListener {
            presenter.doPlayPre()
        }

    }


    override fun onDestroy() {
        super.onDestroy()
//        presenter?.let {
//            it.unRegisterCallBack(this)
//        }



    }

//    override fun onTitleChange(title: String) {
//        binding.playTitle.text=title
//    }
//
//    override fun onProgressChange(current: Int) {
//
//    }
//
//    override fun onPlaying() {
//        binding.playOrPause.text="暂停"
//
//    }
//
//    override fun onPlayPause() {
//        binding.playOrPause.text="播放"
//
//    }
//
//    override fun onCoverChange(cover: String) {
//        println("封面改变了${cover}")
//    }

    /**
     * 监听数据
     */
    private fun initDataListener() {

        presenter.currentMusic.addListener(this) {
            //数据变化  更新UI
            binding.playTitle.text=it?.name
            println("封面改变...${it?.name}...${it?.cover}")
        }

        presenter.currentPlayState.addListener(this) {
            when(it){
                PlayerPresenter.PlayState.PAUSE->{
                    binding.playOrPause.text="播放"
                }
                PlayerPresenter.PlayState.PLAYING->{
                    binding.playOrPause.text="暂停"
                }
            }
        }
    }




}